package com.module.itemeditor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.phprpc.util.AssocArray;
import org.phprpc.util.Cast;
import org.phprpc.util.PHPSerializer;

import com.game.database.MysqlHelper;
import com.sun.org.apache.bcel.internal.generic.CASTORE;

public class CfgItem implements Serializable {
	private static final long serialVersionUID = 6511313480897169955L;
	public static CfgItem createCfgItem(int id,List attrs){
		Object[] parms = new Object[3];
		parms[0] = id;
		CfgItem item = null;
		try {
			item = new CfgItem();
			item.id = id;
			item.attrs = attrs;
			parms[1] = new PHPSerializer().serialize(item);
			parms[2] = parms[1];
			MysqlHelper.ExecuteInsert("insert into cfg_item (`id`,`data`) values (?,?) on duplicate key update `data` = ?", parms);
			if(EditorManager.is_setup){
				for(CfgItem obj:EditorManager.allCfgItems){
					if(obj.id == id){
						obj.attrs = attrs;
						item = obj;
						EditorManager.allCfgItems.remove(obj);
						break;
					}
				}
				EditorManager.allCfgItems.add(item);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return item;
	}
	public static CfgItem getCfgItem(int id){
		Object[] parms = new Object[1];
		parms[0] = id;
		CfgItem item = null;
		try {
			if(EditorManager.is_setup)
			{
				for(CfgItem obj: EditorManager.allCfgItems){
					if(obj.id == id)
						return obj;
				}
			}
			Object bytes = MysqlHelper.ExecuteScalar("select `data` from cfg_item where `id`=?", parms);
			Object obj = new PHPSerializer().unserialize((byte[])bytes);
			item = (CfgItem)obj;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return item;
	}
	public static List<CfgItem> getAllCfgItem(){
		try {
			if(EditorManager.is_setup)
				return EditorManager.allCfgItems;
			ArrayList<Object[]> all = MysqlHelper.ExecuteReader("select * from cfg_item", null);
			List<CfgItem> list = new ArrayList<CfgItem>();
			for(Object[] row : all){
				CfgItem item = (CfgItem)(new PHPSerializer().unserialize((byte[])row[1]));
				item.id = (Integer)row[0];
				list.add(item);
			}
			return list;
		} catch (Exception e) {
			return null;
		}
	}
	public int id;
	public List<Object> attrs = new ArrayList<Object>();
	
	public Object getProperty(String name){
		for(Object p : attrs){
			if(p instanceof IntegerProperty){
				IntegerProperty ip = (IntegerProperty)p;
				if(ip.getName().equals(name)){
					return ip.getValue();
				}
			}
			else if(p instanceof StringProperty){
				StringProperty sp = (StringProperty)p;
				if(sp.getName().equals(name)){
					return sp.getValue();
				}
			}
		}
		return null;
	}
	
	public void setProperty(String name, Object value){
		for(Object p : attrs){
			if(p instanceof IntegerProperty){
				IntegerProperty ip = (IntegerProperty)p;
				if(ip.getName().equals(name)){
					if(value instanceof Integer){
						ip.setValue((Integer) value);
						return;
					}
				}
			}
			else if(p instanceof StringProperty){
				StringProperty sp = (StringProperty)p;
				if(sp.getName().equals(name)){
					if(value instanceof String){
						sp.setValue((String) value);
						return;
					}
				}
			}
		}
		this.attrs.add(PropertyFactory.create(name, value));
	}
	
	@Override
	public String toString() {
		return "[CfgItem(id="+this.id+",attrs="+this.attrs+")]";
	}
}
